﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DataBaseConnection
{
    public class CMSSQL:PConnOrder
    {
        public CMSSQL()
        { 
        
        }

        public CMSSQL(string _ConnectionString)
            : base(_ConnectionString)
        {

        }        

        #region 需要重写的方法

        public override DataBaseType CurrentBaseType
        {
            get { return DataBaseType.MSSQL; }
        }

        public override IDataParameter GetNewParameter(string parameterName, object Value)
        {            
            SqlParameter par = new SqlParameter(parameterName, Value);
            
            return par;
        }

        public override IDataParameter GetNewParameter(string parameterName, object Value, ParameterDirection OutType)
        {
            SqlParameter par = new SqlParameter(parameterName, Value);
            par.Direction = OutType;
            return par;
        }        

        public override void ConnectionClose()
        {
            if (conn != null || conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
        }
        public override DataSet ExecuteDataSet(string sqlText, IDataParameter[] cmdParms, CommandType comType)
        {
            //对象处理
            PrepareCommand(sqlText, cmdParms, comType);

            try
            {
                //创建SqlDataAdapter对象以及DataSet
                SqlDataAdapter da = new SqlDataAdapter((SqlCommand)cmd);
                DataSet ds = new DataSet();
                //填充ds
                da.Fill(ds);
                //返回ds
                return ds;
            }
            catch (System.Exception)
            {
                throw;
            }            
        }
        public override int ExecuteNonQuery(string sqlText, IDataParameter[] cmdParms, CommandType comType)
        {
            //对象处理
            PrepareCommand( sqlText, cmdParms, comType);
            int result = 0;	//要输出的对象

            try
            {
                result = cmd.ExecuteNonQuery();
            }
            catch (System.Exception)
            {
                throw;
            }            

            return result;

        }
        public override IDataReader ExecuteReader(string sqlText, IDataParameter[] cmdParms, CommandType comType)
        {
            //对象处理
            PrepareCommand( sqlText, cmdParms, comType);
            try
            {
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (System.Exception)
            {
                throw;
            }            

        }
        public override object ExecuteScalar(string sqlText, IDataParameter[] cmdParms, CommandType comType)
        {
            //对象处理
            PrepareCommand( sqlText, cmdParms, comType);
            object result = null;	//要输出的对象

            try
            {
                result = (object)cmd.ExecuteScalar();	//返回查询结果的第一行第一列
            }
            catch (System.Exception)
            {
                throw;
            }            

            return result;

        }        
        public override void TransactionStart()
        {
            if (conn == null)
            {
                conn = new SqlConnection();
                conn.ConnectionString = ConnectionString;
                conn.Open();
            }
            if (cmd == null)
            {
                cmd = new SqlCommand();
            }            
            TransactionKey = Guid.NewGuid().ToString().Substring(0,32);            
            myTrans = ((SqlConnection)conn).BeginTransaction();
            cmd.Transaction = myTrans;
            ((SqlTransaction)myTrans).Save(TransactionKey);
        }
        public override void TransactionEnd()
        {
            try
            {
                myTrans.Commit();
            }
            catch (Exception ex)
            {
                ((SqlTransaction)myTrans).Rollback(TransactionKey);
                throw new Exception("操作失败，成功回滚!错误信息：" + ex.Message);
            }            
        }

        public override DataSet SplitPage(string sqlText, string Primary, string Orderby, int MaxRows, int CurrPage)
        {
            if (CurrPage < 1) { CurrPage = 1; }
            IDataParameter[] paras = {
		        GetNewParameter("@SQL", sqlText),
		        GetNewParameter("@Page", CurrPage),
		        GetNewParameter("@RecsPerPage", MaxRows),
		        GetNewParameter("@ID", Primary),
		        GetNewParameter("@Sort", Orderby)
	            };
            return ExecuteDataSet("spAll_ReturnRows", paras, CommandType.StoredProcedure);
        }
        #endregion        

        #region 私有方法
        /// <summary>
        /// 集中处理连接属性
        /// </summary>        
        protected override void PrepareCommand(string cmdText, IDataParameter[] cmdParms, CommandType ct)
        {
            if (conn == null) { conn = new SqlConnection(); }
            if (cmd == null) { cmd = new SqlCommand(); }
            base.PrepareCommand(cmdText, cmdParms, ct);
        }

        #endregion
    }
}
